package net.maku.utils;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Map;
import java.util.Map.Entry;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.fasterxml.jackson.databind.ObjectMapper;

import jakarta.servlet.http.HttpServletRequest;
import net.maku.bean.model.MyException;


public class ReqUtils {
	public static void printHeader( HttpServletRequest req ) {
		Enumeration<String> days = req.getHeaderNames();
		String key="";
		while (days.hasMoreElements()){
			key= days.nextElement();
			System.out.println( "head " + key +" : "+ req.getHeader(key) ); 
		}
	}
	
	public static void printParameter( HttpServletRequest req ) {
		Map<String, String[]> map = req.getParameterMap();
		for ( Entry<String, String[]> entry : map.entrySet() ) {
			if( entry.getValue().length>0) {
				System.out.println( "request paras:  "+entry.getKey()+" : "+entry.getValue()[0] );
			}
		}
	}

	public static void printAll() {
		HttpServletRequest req = getRequestAttributes().getRequest();
		printHeader( req );
		printParameter( req );
	} 
	
	public static void printAll( HttpServletRequest req ) {
		printHeader( req );
		printParameter( req );
	}

	public static String getParameter( HttpServletRequest req, 
			String name, String def ) {
		return ServletRequestUtils.getStringParameter( req, name, def ).trim();
	}
	
	public static String getParameter( String name, String def ) {
		String para = getParameter( name );
		if( !StringUtils.hasText(para) ) {
			para = def.trim();
		}
		return para;
	}
	
	public static String getParameter( String name ) {
		ServletRequestAttributes attr =  getRequestAttributes();
		return ServletRequestUtils.getStringParameter( attr.getRequest(), name, "").trim();
	}
	
	public static <T> T getIoParameter( HttpServletRequest req, Class<T> clz ) {
		try {
			return new ObjectMapper().readValue( 
					req.getInputStream(), clz );
		} catch ( Exception e) {
			e.printStackTrace();
			throw new MyException("获取参数请求失败");
		}
	}
	
	public static <T> T getIoParameter( Class<T> clz ) {
		try {
			ServletRequestAttributes attr =  getRequestAttributes();
			return new ObjectMapper().readValue( 
					attr.getRequest().getInputStream(), clz );
		} catch ( Exception e) {
			e.printStackTrace();
			throw new MyException("获取参数请求失败");
		}
	}
	
	
	public static ServletRequestAttributes getRequestAttributes(){
		RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
		return (ServletRequestAttributes) attributes;
    }
	
	public static String getHeader( String name ) {
		ServletRequestAttributes attr =  getRequestAttributes();
		String para = attr.getRequest().getHeader(name);
		//参数是否为空
		if( !StringUtils.hasText(para) ) {
			para = "";
		}
		return para;
	}
	
	public static String getHeader( HttpServletRequest req, 
			String name, String def ) {
		String para = req.getHeader(name);
		//参数是否为空
		if( !StringUtils.hasText(para) ) {
			para = def.trim();
		}
		return para;
	}

	public static String readInputStream(InputStream inStream) throws Exception {
        ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len;
        while((len = inStream.read(buffer)) != -1) {
            outSteam.write(buffer, 0, len);
        }
        outSteam.close();
        inStream.close();
        return new String( outSteam.toByteArray());
    }


}




